package com.googlecode.intellimars.ui;

import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManagerAdapter;

import java.util.LinkedHashMap;
import java.util.Map;

public class TabbedToolWindow extends ContentManagerAdapter {
    private Map<String, Content> panels = new LinkedHashMap<String, Content>();

    private ToolWindow ideaToolWindow;

    public TabbedToolWindow(String name, ToolWindowManager windowManager, boolean canClose,ToolWindowAnchor anchor) {
        this.ideaToolWindow = windowManager.registerToolWindow(name, canClose, anchor);
    }

    public void registerPanel(String panelName, Content content) {
        Content oldContent = panels.put(panelName, content);
        if (oldContent != null) {
            ideaToolWindow.getContentManager().removeContent(oldContent, true);
        }
        ideaToolWindow.getContentManager().addContent(panels.get(panelName));
    }

    public void startTabChangeListener() {
        ideaToolWindow.getContentManager().addContentManagerListener(this);
    }
    public void stopTabChangeListener() {
        ideaToolWindow.getContentManager().removeContentManagerListener(this);
    }

    public void focusPanelIfExists(String name) {
        Content content = ideaToolWindow.getContentManager().findContent(name);
        if (content != null) {
            ideaToolWindow.getContentManager().setSelectedContent(content);
        }
    }
}
